package cn.edu.ctbu.dp2.service;

import cn.edu.ctbu.dp2.constant.REnum;
import cn.edu.ctbu.dp2.dao.TeacherRepository;
import cn.edu.ctbu.dp2.entity.Teacher;
import cn.edu.ctbu.dp2.exception.RException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.rmi.StubNotFoundException;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;
@Service
public class TeacherService {

    @Autowired
    public TeacherRepository teacherRepository;


      /**
       * 读所有的学生数据
       * @return List Teacher
       */
      public List<Teacher> getAll() {
          return teacherRepository.findAll();
      }

    public Page<Teacher> getAll(Pageable pageable) {
          return teacherRepository.findAll(pageable);
    }
    public Page<Teacher> getAll(Example<Teacher> example, Pageable pageable) {
          return teacherRepository.findAll(example,pageable);
    }

    /**
     * 新增老师
     * @param teacher 对象
     */
    public Teacher add(Teacher teacher){

        return teacherRepository.save(teacher);
    }

    /**
     * 更新学生
     * @param teacher 对象
     */
    public Teacher update(Teacher teacher){
         return teacherRepository.save(teacher);
    }
/**
     * 删除数据
     * @param id 根据id删除数据
     */
public void deleteMultiple(List<Integer> ids) {
    for (Integer id : ids) {
        teacherRepository.deleteById(id);
    }
}

    public Teacher findById(int id){
        return teacherRepository.findById(id).get();
    }

    public Teacher validateUsernameAndPassword(String tno, String password)throws Exception{
        List<Teacher>teachers=teacherRepository.findByTno(tno);
        if(teachers.size()>0){
            Teacher teacher=teachers.get(0);
            if(teacher.getPassword().equals(password)){
                //成功
                return teacher;
            }
            else{
                throw new RException(REnum.LOGIN_ERR);
            }
        }
        else{
            throw new RException(REnum.LOGIN_ERR);
        }
    }
  }





